package com.zrf.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.zrf.hibernate.model.PageBean;
import com.zrf.hibernate.pojo.Product;


@Repository("productDao")
public class ProductDaoImpl extends BaseDaoImpl<Product>{

	private Session session = null;
	private Query query = null;
	private Transaction transaction = null;
	
	public Long getAllNum(){
		Long num = 0l;
		String hql = "SELECT COUNT(p.productid) FROM Product p";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			num = (Long) query.uniqueResult();
			if(num==null){
				num = 0l;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
		return num;
	}
	
	
	
	
	public void updateProduct(Product p){
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.update(p);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		if(session!=null && session.isOpen()){
			session.close();
		}
	}
	
	public void addProduct(Product p){
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.saveOrUpdate(p);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
	}
	
	
	public List<?> getAllId(){
		List<?> list = null;
		String hql = "SELECT p.productid FROM Product p ";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			list = query.list();
			if(list!=null && list.size()<=0){
				list = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
		return list;
	}
	
	public Long getNumBySName(String name){
		Long num = 0l;
		String hql = "SELECT COUNT(p.productid) FROM Product p WHERE p.seller.sname=?";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			query.setString(0, name);
			num = (Long) query.uniqueResult();
			if(num==null){
				num = 0l;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
		return num;
	}
	
	
	
	public List<?> getProductByName(String name,PageBean pagebean){
		List<?> list = null;
		String hql = "FROM Product p WHERE p.seller.sname=?";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			query.setString(0, name);
			query.setMaxResults(pagebean.getPageSize());
			query.setFirstResult(pagebean.getLimit());
			list = query.list();
			if(list!=null && list.size()<=0){
				list = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
		return list;
	}
	
	
	
	
	
	
	
}
